package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
//    @Autowired
//    private AuthenticationManager authenticationManager;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
//        Authentication authentication = null;
//        try {
//            authentication = authenticationManager.authenticate(
//                    new UsernamePasswordAuthenticationToken(
//                            employeeLoginDTO.getUsername(),
//                            employeeLoginDTO.getPassword()
//                    )
//            );
//        } catch (AuthenticationException e) {
//            return Result.error("用户名或密码错误"); // 统一认证失败消息
//        }
//        SecurityContextHolder.getContext().setAuthentication(authentication);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
//        String token = JwtTokenUtils.createToken(employeeLoginDTO.getUsername());
//        Employee employee= (Employee)authentication.getPrincipal();
        EmployeeLoginVO employeeLoginVO = new EmployeeLoginVO();
        BeanUtils.copyProperties(employee, employeeLoginVO);
        employeeLoginVO.setToken(token);
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /*
    * 新增员工
    * */
    @PostMapping("/add")
    @ApiOperation("新增员工")
    public Result<String> add(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.add(employeeDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询员工")
    public Result<PageResult> pageFind(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询员工，参数：{}", employeePageQueryDTO);
        PageResult result =employeeService.pageFind(employeePageQueryDTO);
        return Result.success(result);
    }
    @PostMapping("/status/{status}")
    @ApiOperation("改变员工状态")
    public Result<String> updateStatus(@RequestParam(name = "id") Long id,@PathVariable("status") Integer status) {
        log.info("禁用/启用员工,员工id：{}，当前员工状态，{}", id,status);
        employeeService.changeStatus(id,status);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("查询员工信息")
    public Result<Employee> get(@PathVariable("id") Long id) {
        log.info("根据Id查询员工信息，id：{}",id);
        Employee employee= employeeService.findById(id);
        return Result.success(employee);
    }
    @PutMapping()
    @ApiOperation("编辑员工信息")
    public Result<String> update(@RequestBody EmployeeDTO employee) {
        log.info("修改员工信息,员工信息：{}", employee);
        employeeService.updateinfo(employee);
        return Result.success();
    }
}
